package com.zmn.mcc.admin.controller.common;

import com.zmn.base.channel.common.dro.ChannelDRO;
import com.zmn.base.channel.common.query.ChannelQuery;
import com.zmn.base.channel.dubbo.interfaces.channel.ChannelListRemoteService;
import com.zmn.base.plat.engine.common.constant.DubboConsts;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.AMISListData;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.admin.controller.MccBaseController;
import com.zmn.mcc.model.entity.dpermit.channel.BaseChannelQuery;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/05/26 14:56
 */
@Controller
@RequestMapping("common/channel/")
public class CommonChannelListController extends MccBaseController {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ChannelListRemoteService channelListRemoteService;


    /**
     * 渠道查询
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,            // 主ID，用于回调方法读写相关数据
                       @RequestParam(value = "callbackUrl", defaultValue = "") String callbackUrl,  // 回调方法的url
                       @RequestParam(value = "multi", defaultValue = "1") Integer multi,            // 是否允许多选，1不允许，2允许
                       @ModelAttribute BaseChannelQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        ChannelQuery channelQuery = new ChannelQuery();
        if(StringUtil.isNotBlank(keyword)){
            channelQuery.setKeyword(keyword);
        }
        channelQuery.setPageIndex(query.getPageIndex());
        channelQuery.setPageSize(query.getPageSize());
        channelQuery.setRank(NumberUtil.isNullOrZero(query.getLevel())?null:query.getLevel());
        channelQuery.setStatus(NumberUtil.isNullOrZero(query.getStatus())?null:query.getStatus());
        ResponseDTO<AMISListData<ChannelDRO>> responseDTO = channelListRemoteService.listPageByQuery(channelQuery);
        List<ChannelDRO> items = new ArrayList<>();
        if (responseDTO.isSuccess()) {
            AMISListData<ChannelDRO> channelListPageDRO = responseDTO.getData();
            query.setTotalCount(channelListPageDRO.getTotal());
            items.addAll(channelListPageDRO.getItems());
        }


        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("keyword", keyword);

        model.addAttribute("keyId", keyId);
        model.addAttribute("callbackUrl", callbackUrl);
        model.addAttribute("multi", multi);
        model.addAttribute("type", query.getType());

        return "common/channel/list";
    }


    /**
     * 合作查询
     */
    @RequestMapping(value = "list/cooperation")
    public String listCooperation(HttpServletRequest request,
                       @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,            // 主ID，用于回调方法读写相关数据
                       @RequestParam(value = "callbackUrl", defaultValue = "") String callbackUrl,  // 回调方法的url
                       @RequestParam(value = "multi", defaultValue = "1") Integer multi,            // 是否允许多选，1不允许，2允许
                       @ModelAttribute BaseChannelQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        ChannelQuery channelQuery = new ChannelQuery();
        if(StringUtil.isNotBlank(keyword)){
            channelQuery.setKeyword(keyword);
        }
        channelQuery.setPageIndex(query.getPageIndex());
        channelQuery.setPageSize(query.getPageSize());
        channelQuery.setRank(NumberUtil.isNullOrZero(query.getLevel())?null:query.getLevel());
        channelQuery.setStatus(NumberUtil.isNullOrZero(query.getStatus())?null:query.getStatus());
        ResponseDTO<AMISListData<ChannelDRO>> responseDTO = channelListRemoteService.listPageByQuery(channelQuery);
        List<ChannelDRO> items = new ArrayList<>();
        if (responseDTO.isSuccess()) {
            AMISListData<ChannelDRO> channelListPageDRO = responseDTO.getData();
            query.setTotalCount(channelListPageDRO.getTotal());
            items.addAll(channelListPageDRO.getItems());
        }


        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("keyword", keyword);

        model.addAttribute("keyId", keyId);
        model.addAttribute("callbackUrl", callbackUrl);
        model.addAttribute("multi", multi);
        model.addAttribute("type", query.getType());

        return "common/cooperation/list";
    }
}
